// Demonstrates miniz.c's compress() and uncompress() functions
// (same as zlib's). Public domain, May 15 2011, Rich Geldreich,
// richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.

#include "compress.h"
#include <stdio.h>
#include <string.h>
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;

int main(int argc, char *argv[]) {
    // file io about
    FILE * p_src = NULL;
    FILE * p_dest = NULL;
    FILE * p_src2 = NULL;
    FILE * p_dest2 = NULL;
    char s_pStr[100] = {0};
    unsigned char pCmp[100] = {0};
    unsigned char pUncomp[100] = {0};
    unsigned int array[10000] = {0};
    unsigned int src_len_array[10000] = {0};
    int cnt = 0;
    //char s_pStr[100] = {0};
    int size = 0;
    // arg
    const char* path_s = "source";
    const char* path_c = "source_c";
    const char* path_d = "source_d";
    // compress about
    int cmp_status;
    uLong src_len;
    uLong cmp_len;
    uLong uncomp_len;
    uint total_succeeded = 0;

    printf("miniz.c version: %s\n", MZ_VERSION);
#if 1
    p_src = fopen(path_s,"rb");
    if(!p_src) {  return 0; } 
    p_dest = fopen(path_c, "wb");
    if(!p_dest)
    {
        fclose(p_src);  p_src = NULL;  return 0;
    }

    while(1)
    {
        size = fread(s_pStr, sizeof(char), sizeof(s_pStr), p_src);
        if( !size ){ break; }

        src_len = (uLong)size;
        cmp_len = compressBound(src_len);
        // Compress the string.
        cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)s_pStr, src_len);
        if (cmp_status != Z_OK) {
            goto err_c;
        }
        printf("Compressed from %u to %u bytes\n", (mz_uint32)src_len, (mz_uint32)cmp_len);
        array[cnt] = cmp_len;
        src_len_array[cnt] =  size;
        fwrite(pCmp, sizeof(char), cmp_len, p_dest);
        cnt++;
    }
    fclose(p_dest);
    fclose(p_src);
#endif

    cnt = 0;
    p_src2 = fopen(path_c,"rb");
    if(!p_src2) {  return 0; } 
    p_dest2 = fopen(path_d, "wb");
    if(!p_dest)
    {
        fclose(p_src2);  p_src2 = NULL;  return 0;
    }

    for(cnt = 0; !size;)
    {
        uncomp_len = src_len_array[cnt];
        cmp_len = array[cnt];
        size = fread(pCmp, sizeof(char), cmp_len, p_src2);
        if( !size ){ break; }
        // Decompress.
        cmp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);
        total_succeeded += (cmp_status == Z_OK);

        printf("Decompressed from %u to %u bytes\n", (mz_uint32)cmp_len,
               (mz_uint32)uncomp_len);
        if (cmp_status != Z_OK) {
            goto err_d;
        }

        // Ensure uncompress() returned the expected data.
        if (uncomp_len != src_len_array[cnt])
        {
            printf("Decompression failed!\n");
            goto err_d;
        }
        fwrite(pUncomp, sizeof(char), uncomp_len, p_dest2);

        cnt ++;
    }

    printf("Success.\n");
    return EXIT_SUCCESS;
err_c:
    fclose(p_dest);
    fclose(p_src);
    p_dest = NULL;
    p_src = NULL;
    return EXIT_FAILURE;
err_d:
    fclose(p_dest2);
    fclose(p_src2);
    p_dest2 = NULL;
    p_src2 = NULL;
    return EXIT_FAILURE;
}
